﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChiTietMonAnDAO
    {
        public static bool Insert(ChiTietMonAnDTO ctma)
        {
            bool result = false;

            string strSQLEmployee = "";
            int n = 0;

            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MonAn", ctma.MonAn));
                sqlParams.Add(new SqlParameter("@NVL", ctma.NVL));

                strSQLEmployee = "Select SoLuong From ChiTietMonAn Where MonAn=@MonAn and NVL=@NVL";
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSQLEmployee, sqlParams);

                if (dt.Rows.Count == 0)
                {
                    strSQLEmployee = "Insert into ChiTietMonAn(MonAn,NVL,DonViTinh,SoLuong) values (@MonAn,@NVL,@DonViTinh,@SoLuong)";
                    // Create List Sql Parameter
                    sqlParams = new List<SqlParameter>();
                    sqlParams.Add(new SqlParameter("@MonAn", ctma.MonAn));
                    sqlParams.Add(new SqlParameter("@NVL", ctma.NVL));
                    sqlParams.Add(new SqlParameter("@DonViTinh", ctma.DonViTinh));
                    sqlParams.Add(new SqlParameter("@SoLuong", ctma.SoLuong));
                    // Call execute none query
                    n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                }
                else
                {
                    strSQLEmployee = "Update ChiTietMonAn set DonViTinh=@DonViTinh,SoLuong=@SoLuong " +
                        "Where MonAn=@MonAn and NVL=@NVL";
                    // Create List Sql Parameter
                    sqlParams = new List<SqlParameter>();
                    sqlParams.Add(new SqlParameter("@MonAn", ctma.MonAn));
                    sqlParams.Add(new SqlParameter("@NVL", ctma.NVL));
                    sqlParams.Add(new SqlParameter("@DonViTinh", ctma.DonViTinh));
                    sqlParams.Add(new SqlParameter("@SoLuong", ctma.SoLuong + Convert.ToInt32(dt.Rows[0]["SoLuong"].ToString())));
                    // Call execute none query
                    n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);//.ExecuteNoneQuery(strSQLEmployee, sqlParams);
                }

                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool Delete(int ID)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM ChiTietMonAn WHERE ID=@ID";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@ID", ID));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool DeleteByMaMon(int MaMA)
        {
            bool result = false;
            string strSQLEmployee = "DELETE FROM ChiTietMonAn WHERE MonAn=@MonAn";
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MonAn", MaMA));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExcuteNoneQuery(strSQLEmployee, sqlParams);
                if (n >= 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static DataTable SelectByMaMonAn(int MaMonAn)
        {
            String strSql = "Select ID, NVL.MaNVL, TenNVL, CTMA.DonViTinh, CTMA.SoLuong " +
                "From NguyenVatLieu NVL, ChiTietMonAn CTMA " +
                "Where MonAn = @MaMonAn and NVL.MaNVL = CTMA.NVL";

            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaMonAn", MaMonAn));

                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql, sqlParams);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
